package org.umlviewer.model.diagram_class;

import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import org.umlviewer.model.Constants;
import org.umlviewer.model.DiagramElement;
import org.umlviewer.model.Relationship;
import org.umlviewer.paint.ArrowHead;
import org.umlviewer.paint.ArrowHeadDiamond;
import org.umlviewer.paint.ArrowHeadHalfTriangle;
import org.umlviewer.paint.ArrowHeadTriangle;

/**
 *
 * @author Rafa
 */
public class Association extends Relationship{           
    
    public Association(){
        super();
    }
    
    public Association(String label, int type, DiagramElement source, DiagramElement destination) {
        super(label,type,source,destination);
    }        
    
    public void paint(Graphics g2)
    {
        double dstX, dstY;
        double srcX, srcY;        
        double midX, midY;                
        Line2D linea;
        
        Graphics2D g = (Graphics2D)g2;        
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
       
        Point2D q = getSource().getRelPoint(getDestination().getMidPoint());
        srcX = q.getX();
        srcY = q.getY(); 
        
        Point2D p = getDestination().getRelPoint(getSource().getMidPoint());
        dstX = p.getX();
        dstY = p.getY();                
        
        ArrowHead head=null;
        Color fillColor=Color.black;
        //original stroke
        Stroke originalStroke = g.getStroke();
        Stroke dotted = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {10}, 0);
                
        //linea punteada
        if(getType() == REALITATION || getType() == DEPENDENCY || getType() == INCLUDE || getType() == EXTEND)
            g.setStroke(dotted);
        
        linea = new Line2D.Double(srcX, srcY, dstX , dstY);
        g.setColor(getLineColor());
        g.draw(linea);
        g.setColor(Color.BLACK);
        //getLine().setLine(linea);
        //se restaura el tipo de linea original
        g.setStroke(originalStroke);
        
        Point pt1 = new Point((int)srcX,(int)srcY);
        Point pt2 = new Point((int)dstX,(int)dstY);         

        //using GEF library from trigis http://gef.tigris.org/ to print end of edge
        
        switch(getType())
        {
            case GENERALIZATION: case REALITATION:
                head = new ArrowHeadTriangle();
                fillColor = Color.white;
                break;
            case COMPOSITION:
                head = new ArrowHeadDiamond();
                break;
            case AGGREGATION:
                head = new ArrowHeadDiamond();                
                fillColor = Color.white;
                break;            
            case DEPENDENCY: case INCLUDE : case EXTEND:
                head = new ArrowHeadHalfTriangle();
                break;
        }                  
        if(head!=null)
            head.paint(g, pt1, pt2, getLineColor(), fillColor);
        
        //nombre de la relacion, se calcula la mitad de la linea
        midX = (dstX - srcX) /2;
        midY = (dstY - srcY) /2;
        g.drawString(this.getName(), (int)srcX + (int)midX, (int)srcY + (int)midY);
        //multiplicidad
        if( getSrcMultiplicity()!= null && ! getSrcMultiplicity().equals(""))
            g.drawString( getSrcMultiplicity(), (int)srcX-5, (int)srcY-10);
        if(getDstMultiplicity()!= null && !getDstMultiplicity().equals(""))
            g.drawString(getDstMultiplicity(), (int)dstX-5, (int)dstY-10);
        
        //si la linea esta seleccionada
        if(isSelected())
        {
            g.setColor(Constants.SELECTIONCOLOR);
            g.fillRect(pt1.x, pt1.y, 5, 5);
            g.fillRect(pt2.x, pt2.y, 5, 5);
            g.setColor(Constants.DEFAULTCOLOR);
        }
        
    }
    
    
}
